package com.hutao.manage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hutao.manage.datasource.TargetDataSource;
import com.hutao.manage.mapper.BaseInfoMapper;
import com.hutao.manage.mapper.MedicalPrinterRangeMapper;
import com.hutao.manage.pojo.MedicalPrinterRange;
import com.hutao.manage.service.BaseInfoService;


@Service
public class BaseInfoServiceImpl implements BaseInfoService {

	private String hospitalCode = "49557184-0";
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Autowired
	private MedicalPrinterRangeMapper medicalPrinterRangeMapper;
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getHospitalByCode(String hospitalCode) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getHospitalByCode(hospitalCode);
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageBasic() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> medicalPayMentTypes = baseInfoMapper.getMedicalPaymentTypes();
		
		List<Map<String, Object>> sexs = baseInfoMapper.getsSexs();
		
		List<Map<String, Object>> marriages = baseInfoMapper.getMarriages();
		
		List<Map<String, Object>> personalTypes = baseInfoMapper.getPersonalType();
		
		baseInfo.put("medicalPayMentTypes", medicalPayMentTypes);
		baseInfo.put("sexs", sexs);
		baseInfo.put("marriages", marriages);
		baseInfo.put("personalTypes", personalTypes);
		
		List<Map<String, Object>> idDocumentTypes = baseInfoMapper.getIdDocumentTypes();
		baseInfo.put("idDocumentTypes", idDocumentTypes);
		
		List<Map<String, Object>> nationalitys = baseInfoMapper.getNationalitys();
		baseInfo.put("nationalitys", nationalitys);
		
		List<Map<String, Object>> nations = baseInfoMapper.getNations();
		baseInfo.put("nations", nations);
		
		
		List<Map<String, Object>> tjIdentifys = baseInfoMapper.getTjIdentifys();
		baseInfo.put("tjIdentifys", tjIdentifys);
		
		List<Map<String, Object>> tjTopUnits = baseInfoMapper.getTjTopUnits();
		baseInfo.put("tjTopUnits", tjTopUnits);
		
		List<Map<String, Object>> tjDutys = baseInfoMapper.getTjDutys();
		baseInfo.put("tjDutys", tjDutys);
		
		List<Map<String, Object>> tjChargeTypes = baseInfoMapper.getTjChargeTypes();
		baseInfo.put("tjChargeTypes", tjChargeTypes);
		
		List<Map<String, Object>> professions = baseInfoMapper.getProfessions();
		baseInfo.put("professions", professions);
		
		List<Map<String, Object>> relativeRelations = baseInfoMapper.getRelativeRelations();
		baseInfo.put("relativeRelations", relativeRelations);
		
		List<Map<String, Object>> inHospitalTypes = baseInfoMapper.getInHospitalTypes();
		baseInfo.put("inHospitalTypes", inHospitalTypes);
		
		List<Map<String, Object>> inHospitalStates = baseInfoMapper.getInHospitalStates();
		baseInfo.put("inHospitalStates", inHospitalStates);
		
		List<Map<String, Object>> outHospitalTypes = baseInfoMapper.getOutHospitalTypes();
		baseInfo.put("outHospitalTypes", outHospitalTypes);
		
		List<Map<String, Object>> rehospitalAims = baseInfoMapper.getRehospitalAims();
		baseInfo.put("rehospitalAims", rehospitalAims);
		
		List<Map<String, Object>> hospitalDealthReasons = baseInfoMapper.getHospitalDealthReasons();
		baseInfo.put("hospitalDealthReasons", hospitalDealthReasons);
		
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		if(!hospitals.isEmpty()){
			medicalDepts = baseInfoMapper.getMedicalDeptByHospitalId(((Integer)hospitals.get(0).get("id")));
		}
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getDiseaseByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getDiseaseByQueryName("%"+queryName+"%");
	}
	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getMedicalWorkerByQueryName(
			String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getMedicalWorkerByQueryName("%"+queryName+"%");
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageDiag() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> diagTypes = baseInfoMapper.getDiagTypes();
		baseInfo.put("diagTypes", diagTypes);
		
		List<Map<String, Object>> treatmentResults = baseInfoMapper.getTreatmentResults();
		baseInfo.put("treatmentResults", treatmentResults);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageCure() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> bloodTypes = baseInfoMapper.getBloodTypes();
		baseInfo.put("bloodTypes", bloodTypes);
		
		List<Map<String, Object>> rhBloodTypes = baseInfoMapper.getRhBloodTypes();
		baseInfo.put("rhBloodTypes", rhBloodTypes);
		
		List<Map<String, Object>> diagAccordTypes = baseInfoMapper.getDiagAccordTypes();
		baseInfo.put("diagAccordTypes", diagAccordTypes);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageOperate() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> incisionLevels = baseInfoMapper.getIncisionLevels();
		baseInfo.put("incisionLevels", incisionLevels);
		List<Map<String, Object>> cicatrizeTypes = baseInfoMapper.getCicatrizeTypes();
		baseInfo.put("cicatrizeTypes", cicatrizeTypes);
		List<Map<String, Object>> opsLevels = baseInfoMapper.getOpsLevels();
		baseInfo.put("opsLevels", opsLevels);
		
		
		List<Map<String, Object>> anaesthesiaWays = baseInfoMapper.getAnaesthesiaWays();
		baseInfo.put("anaesthesiaWays", anaesthesiaWays);
		
		
		List<Map<String, Object>> anaesthesiaLevels = baseInfoMapper.getAnaesthesiaLevels();
		baseInfo.put("anaesthesiaLevels", anaesthesiaLevels);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getOperateByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getOperateByQueryName("%"+queryName+"%");
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageTransferDept() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		if(!hospitals.isEmpty()){
			medicalDepts = baseInfoMapper.getMedicalDeptByHospitalId(((Integer)hospitals.get(0).get("id")));
		}
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageInfusionBlood() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> bloodTypes = baseInfoMapper.getBloodTypes();
		baseInfo.put("bloodTypes", bloodTypes);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageIntensiveCare() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> ICUTypes = baseInfoMapper.getICUTypes();
		baseInfo.put("ICUTypes", ICUTypes);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageAllergyDrug() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> allergyDrugTypes = baseInfoMapper.getAllergyDrugTypes();
		baseInfo.put("allergyDrugTypes", allergyDrugTypes);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePagePressureSore() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> pressureSoreCradles = baseInfoMapper.getPressureSoreCradles();
		baseInfo.put("pressureSoreCradles", pressureSoreCradles);
		
		List<Map<String, Object>> pressureSorePhases = baseInfoMapper.getPressureSorePhases();
		baseInfo.put("pressureSorePhases", pressureSorePhases);
		
		List<Map<String, Object>> pressureSoreParts = baseInfoMapper.getPressureSoreParts();
		baseInfo.put("pressureSoreParts", pressureSoreParts);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageInfection() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> pathogens = baseInfoMapper.getPathogens();
		baseInfo.put("pathogens", pathogens);
		
		List<Map<String, Object>> pathogenyCheckSpecimens = baseInfoMapper.getPathogenyCheckSpecimens();
		baseInfo.put("pathogenyCheckSpecimens", pathogenyCheckSpecimens);
		
		List<Map<String, Object>> predisposeFactors = baseInfoMapper.getPredisposeFactors();
		baseInfo.put("predisposeFactors", predisposeFactors);
		return baseInfo;
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageAntibacterialDrug() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> pathogens = baseInfoMapper.getPathogens();
		baseInfo.put("pathogens", pathogens);
		List<Map<String, Object>> pathogenyCheckSpecimens = baseInfoMapper.getPathogenyCheckSpecimens();
		baseInfo.put("pathogenyCheckSpecimens", pathogenyCheckSpecimens);
		
		List<Map<String, Object>> antibacterialDrugPurposes = baseInfoMapper.getAntibacterialDrugPurposes();
		baseInfo.put("antibacterialDrugPurposes", antibacterialDrugPurposes);
		List<Map<String, Object>> antibacterialDrugClassificatorys = baseInfoMapper.getAntibacterialDrugClassificatorys();
		baseInfo.put("antibacterialDrugClassificatorys",antibacterialDrugClassificatorys);
		
		List<Map<String, Object>> antibacterialDrugRegimens = baseInfoMapper.getAntibacterialDrugRegimens();
		baseInfo.put("antibacterialDrugRegimens", antibacterialDrugRegimens);
		
		return baseInfo;
	}

	
	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPrintData(Integer mrPrintTypeId) {
		
		List<Map<String,Object>> mrPageTypeList = baseInfoMapper.getMrPageType();
		
		List<Map<String,Object>> mrChoosePageTypeList = baseInfoMapper.getChooseMrPageType(mrPrintTypeId);
		for(Map<String,Object> mrPageType :  mrPageTypeList){
			boolean flag = false;
			Integer  mrPageTypeId = (Integer) mrPageType.get("id");
			for (Map<String, Object> mrChoosePageType : mrChoosePageTypeList) {
				Integer mrChoosePageTypeId = (Integer) mrChoosePageType.get("pageTypeId");
				if(mrPageTypeId==mrChoosePageTypeId){
					flag=true;
				}
			}
			mrPageType.put("flag", flag);
		}
		
		
		return mrPageTypeList;
	}

	

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getPrintTypeList() {
		return baseInfoMapper.getPrinterType();
	}

	@TargetDataSource(name="ds1")
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void addPageTypes(String pageTypeIds, Integer mrPrintTypeId) {
	
		baseInfoMapper.deleteMrPagePrintType(mrPrintTypeId);
		if(StringUtils.isNotEmpty(pageTypeIds)){
			MedicalPrinterRange medicalPrinterRange = new MedicalPrinterRange();
			for(String pageTypeId:pageTypeIds.split(",")){
				medicalPrinterRange.setMrPageTypeId(Integer.parseInt(pageTypeId));
				medicalPrinterRange.setMrPrinterTypeId(mrPrintTypeId);
				medicalPrinterRangeMapper.insert(medicalPrinterRange);
			}
		}
		
	}

	@TargetDataSource(name="ds1")
	@Transactional(readOnly=true)
	@Override
	public List<Map<String, Object>> getChooseOfficeCode(String code) {
		List<Map<String, Object>> offices = baseInfoMapper.getChooseOfficeCode(code);
		return offices;
	}

}
